﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace W1
{
    public partial class zhir_1 : Form
    {
        //添加/修改类别判断值
        public static int temp = 0;

        //选中行的索引值
        public static int index = 0;
        public zhir_1()
        {
            InitializeComponent();
        }

        private void zhir_1_Load(object sender, EventArgs e)
        {
           //添加
            if (temp == 1)
            {
                //设定按钮显示=添加
                button1.Text = "添加";

            }
            //修改
            if (temp == 2)
            {
                //设定按钮显示=修改
                button1.Text = "修改";
                //设定控件文本显示值
                textBox1.Text = zhir.dt.Rows[index]["StuNoId"].ToString();
                textBox2.Text = zhir.dt.Rows[index]["Grade"].ToString();
                textBox3.Text = zhir.dt.Rows[index]["classID"].ToString();
                textBox4.Text = zhir.dt.Rows[index]["Username"].ToString();
                textBox5.Text = zhir.dt.Rows[index]["Time1"].ToString();
                textBox6.Text = zhir.dt.Rows[index]["Location1"].ToString();


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加
            if (temp == 1)
            {
                //接收画面数据
                string name = textBox1.Text;
                string pwd = textBox2.Text;
                string name1 = textBox3.Text;
                string pwd1 = textBox4.Text;
                string name2 = textBox5.Text;
                string pwd2 = textBox6.Text;

                //创建行(Form1.dt数据集)对象
                DataRow dr = zhir.dt.NewRow();
                //设定行内元素值
                dr["StuNoId"] = name;
                dr["Grade"] = pwd;
                dr["classID"] = name1;
                dr["Username"] = pwd1;
                dr["Time1"] = name2;
                dr["Location1"] = pwd2;

                //将行添加到table中
                zhir.dt.Rows.Add(dr);

                //更新数据库
                string sql = string.Format("insert into Weareonduty values ('{0}','{1}','{2}','{3}','{4}','{5}')", name, pwd, name1, pwd1, name2, pwd2);
                bool number = DBHelper.ExecuteNonQuery(sql);
                if (number)
                {
                    MessageBox.Show("添加成功！");
                }

                //本窗体关闭
                this.Close();
            }

            //修改
            if (temp == 2)
            {


                //通过索引值---取得选中行
                DataRow dr = zhir.dt.Rows[index];
                //取得所选行内的更改前的姓名
                string a1 = dr["StuNoId"].ToString();
                //打开行编辑
                dr.BeginEdit();
                //更改行内数据
                dr["StuNoId"] = textBox1.Text;
                dr["Grade"] = textBox2.Text;
                dr["classID"] = textBox3.Text;
                dr["Username"] = textBox4.Text;
                dr["Time1"] = textBox5.Text;
                dr["Location1"] = textBox6.Text;
                //关闭行编辑
                dr.EndEdit();
                //更新数据库
                string sql = string.Format("update Weareonduty set StuNoId='{0}',Grade='{1}',classID='{2}' ,Username='{3}' ,Time1='{4}' ,Location1='{5}'  where StuNoId='{6}'", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, a1);
                bool number = DBHelper.ExecuteNonQuery(sql);
                if (number)
                {
                    MessageBox.Show("修改成功！");
                }

                //本窗体关闭
                this.Close();

            }
        }
    }
}
